package cn.frh.product.service.impl;

import cn.frh.basic.service.impl.BaseServiceImpl;
import cn.frh.log.annotation.Log;
import cn.frh.product.domain.Product;
import cn.frh.product.domain.ProductDetail;
import cn.frh.product.mapper.ProductDetailMapper;
import cn.frh.product.mapper.ProductMapper;
import cn.frh.product.service.IProductService;
import cn.frh.user.domain.Logininfo;
import cn.frh.user.domain.User;
import cn.frh.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 上架
     * @param ids
     */
    @Override
    @Log(value = "上架服务")
    public void onSale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onSaleTime",new Date());
        productMapper.onSale(map);
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    @Log(value = "下架服务")
    public void offSale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offSaleTime",new Date());
        productMapper.offSale(map);
    }
    @Override
    public Product loadByIdDetail(Long id) {
        return productMapper.loadByIdDetail(id);
    }

    @Override
    public void adopt(Long id, Logininfo logininfo) {
        Product product = productMapper.loadByIdDetail(id);
        product.setState(0);
        product.setOffsaletime(new Date());
        if (logininfo!=null){
            User user = userMapper.loadByLogininfoId(logininfo.getId());
            //product.setUser_id(user.getId());
        }
        productMapper.update(product);
    }

    @Override
    @Transactional
    @Log(value = "添加服务")
    public void add(Product product) {
        productMapper.save(product);
        ProductDetail detail = product.getDetail();
        detail.setProduct_id(product.getId());
        productDetailMapper.save(detail);
    }
}
